package com.server;

import com.jfinal.core.Controller;
import com.jfinal.core.Path;
import com.jfinal.kit.Ret;
import com.thread.ApplicationThreadPool;

/**
 * lidecai
 * 测试web服务器
 */

@Path("/webserver")
public class WebServer extends Controller {

    /**
     * lidecai
     * 用于高并发测试
     */
    public void currentRequest() {

        renderJson(Ret.ok());
    }

    /**
     * lidecai
     * 耗时任务
     * 
     * @throws InterruptedException
     */
    public void threadSleep() throws InterruptedException {
        Thread.sleep(1000);
        renderJson(Ret.ok());
    }

    /**
     * lidecai
     * 死循环测试
     */
    public void deadlock() {
        while (true) {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.err.println("放我出去！！！！！");
        }
    }

    /**
     * lidecai
     * 线程阻塞30秒
     */
    public void sleep30Millis() {
        try {
            Thread.sleep(30000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        renderJson(Ret.ok());
    }

    /**
     * lidecai
     * 线程阻塞30秒
     */
    public void sleep10Millis() {
        try {
            Thread.sleep(10000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        renderJson(Ret.ok());
    }

    /**
     * lidecai
     * 存入线程池并睡眠30秒
     */
    public void sleep10MillisThreadPool() {
        ApplicationThreadPool.hreadPoolExecutor.execute(() -> {
            try {
                Thread.sleep(10000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.err.println("放我出去！！！！！");
        });
        renderJson(Ret.ok());
    }

    /**
     * lidecai
     * 向线程池提交死循环任务
     */
    public void threadPoolExecute() {
        ApplicationThreadPool.hreadPoolExecutor.execute(() -> {
            while (true) {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.err.println("放我出去！！！！！");
            }
        });
        renderJson(Ret.ok("msg", "执行了"));
    }

}
